package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.query.PaymentScheduleQueryObject;
import cn.wolfcode.p2p.bussness.service.IPaymentScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ReturnMoneyController {
    @Autowired
    private IPaymentScheduleService paymentScheduleService;
    @Autowired
    private IAccountService accountService;
    @RequestMapping("/borrowBidReturn_list")
    public String returMoneyPage(@ModelAttribute("qo")PaymentScheduleQueryObject qp, Model model){
        qp.setBidRequestCreateUserId(UserContext.getCount().getId());
        model.addAttribute("pageInfo",paymentScheduleService.queryPage(qp));
        model.addAttribute("account",accountService.getCurrent());
        return "/returnmoney_list";
    }

    //立即还款
    @RequestMapping("/returnMoney")
    @ResponseBody
    public AjaxResult returnMoney(Long id){
        AjaxResult result = null;
        try {
            paymentScheduleService.returnMoney(id);
            result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }
}
